package com.rs.controller;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import com.rs.model.CommonResult;
import com.rs.model.ZhEmployment;
import com.rs.model.ZhOvertime;
import com.rs.model.ZhUser;
import com.rs.service.PublicUserMethodsService;
import com.rs.service.ZhUserStudentService;
import com.rs.util.DateFormatUtil;
import com.rs.util.SpringContextUtils;

import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;

import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionEvent;

public class SelectUserAudit extends JInternalFrame {
	
	private int state=1;//1代表 状态为待审核 2为驳回
	private int pageNum=1; //页码
	private int pages;//总也数
	ZhUser user;
	
	SelectUserAudit ss = this;
	
	private JTable table;
	
	private ZhUserStudentService zhUserService = SpringContextUtils.getBean(ZhUserStudentService.class);

	private PublicUserMethodsService pum = SpringContextUtils.getBean(PublicUserMethodsService.class);
	
	private Integer kk=null;
	private JComboBox comboBox_state;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SelectUserAudit frame = new SelectUserAudit();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SelectUserAudit() {
		aa();
	}
	public SelectUserAudit(ZhUser user) {
			this.user=user;
			aa();
	
		}
	
	void aa(){
		setMaximizable(true);
		setIconifiable(true);
		setClosable(true);
		setBounds(100, 100, 602, 580);
		getContentPane().setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(36, 135, 457, 329);
		getContentPane().add(scrollPane);
		
		table = new JTable();
		table.setRowHeight(30);
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
			},
			new String[] {
				"id", "姓名", "职务", "部门", "性别"
			}
		));
		scrollPane.setViewportView(table);
		
		
		 	// Step1 添加行点击绑定事件 
		  table.addMouseListener(new MouseAdapter() { 
		   @Override 
		   public void mouseClicked(MouseEvent e) {                 
			   rowClicked();   
		    }
		   /** *行点击事件 * 
		   *<br>-----------------------------------------------------<br> *@author flotage 
		   *@create 2013-4-17上午10:09:14 *@note */ 
		   private void rowClicked() {
		    // 获取被点击的行 
		    int row = table.getSelectedRow(); 
		    // 获取点击行的内容 
            DefaultTableModel dtm = (DefaultTableModel) table.getModel();         
            int id  =  (int)dtm.getValueAt(row, 0);         
            UserAuditInfo uu = new UserAuditInfo(id,ss,user);
            uu.setVisible(true);
		   }         
		  });  
		
		
		JLabel label = new JLabel("人员审核");
		label.setFont(new Font("宋体", Font.PLAIN, 16));
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setBounds(257, 32, 69, 26);
		getContentPane().add(label);
		
		JButton button = new JButton("上一页");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				upPage();
			}
		});
		button.setBounds(300, 489, 93, 23);
		getContentPane().add(button);
		
		JButton button_1 = new JButton("下一页");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				upPage();
			}
		});
		button_1.setBounds(403, 489, 93, 23);
		getContentPane().add(button_1);
		
		JButton button_2 = new JButton("待审核列表");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pageNum=1;
				state=1;
				nextPage();
			}
		});
		button_2.setBounds(40, 87, 93, 23);
		getContentPane().add(button_2);
		
		JButton button_3 = new JButton("驳回列表");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pageNum=1;
				state=2;
				nextPage();
			}
		});
		button_3.setBounds(146, 87, 93, 23);
		getContentPane().add(button_3);
		
		/*comboBox_state = new JComboBox();
		comboBox_state.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String str = comboBox_state.getSelectedItem().toString();
				if(str.equals("待审核")){
					state = 1;
				}else if(str.equals("已驳回")){
					state = 2;
				}else if(str.equals("已通过")){
					state = 3;
				}
				pageNum=1;
				nextPage();
			}
		});
		comboBox_state.setModel(new DefaultComboBoxModel(new String[] {"待审核", "已驳回", "已通过"}));
		comboBox_state.setBounds(25, 64, 93, 21);
		getContentPane().add(comboBox_state);*/
		
		JButton button_4 = new JButton("刷新");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				shuaxin();
			}
		});
		button_4.setBounds(403, 87, 93, 23);
		getContentPane().add(button_4);
		
		nextPage();
		
	}
	
	//上一页
	private void upPage(){
		if(pageNum<=2){
			alert("当前是第一页！！");
			return;
		}
		setTheValue(getUserAudit());
		pageNum--;
	}
	
	//下一页
	private void nextPage(){
		if(pageNum >=pages && pageNum !=1){
			alert("当前是最后一页！！");
			return;
		}
		setTheValue(getUserAudit());
		pageNum++;
	}
	
	//得到所有待审核或者驳回的信息
	private List<ZhUser> getUserAudit(){
		if(state ==1){
			return zhUserService.getListAuthstr(pageNum, 0);
		}else if(state ==2){
			return zhUserService.getListAuthstr(pageNum, 2);
		}
		return null;
	}
	
	//数据渲染
	private void setTheValue(List<ZhUser> list){
		TableModel ta = table.getModel();
		for(int i=0;i<ta.getRowCount();i++){
			if(i>=list.size()){
				ta.setValueAt(null, i, 0);
				ta.setValueAt(null, i, 1);
				ta.setValueAt(null, i, 2);
				ta.setValueAt(null, i, 3);
				ta.setValueAt(null, i, 4);
				continue;
			}
			ZhUser ee = list.get(i);
			ta.setValueAt(ee.getuId(), i, 0);
			ta.setValueAt(ee.getuName(), i, 1);
			ta.setValueAt(getDe(ee.getuDeid()), i, 2);
			ta.setValueAt(getPo(ee.getuPoid()), i, 3);
			ta.setValueAt(ee.getuSex(), i, 4);
		}
	}
	
	//根据id得到部门
	private String getDe(Integer id){
		return pum.selectDe(id);
	}
	//根据id得到所有职务
	private String getPo(Integer id){
		return pum.selectPo(id);
	}
	
	//。弹窗
	private void alert(String aa){
		JOptionPane.showMessageDialog(this, aa);
	}
	//页面刷新
	private void shuaxin(){
		pageNum--;
			setTheValue(getUserAudit());
		pageNum++;
	}
	
	
	//动态刷新
	public void dynamicRefresh(Integer id){
		ZhUser user =new ZhUser();
		user.setuId(id);
		CommonResult<ZhUser>  com = zhUserService.selectStudent(user);
		user=com.getData();
		DefaultTableModel ta = (DefaultTableModel)table.getModel();
		for(int i=0;i<table.getRowCount();i++){
			int  tid = (int)ta.getValueAt(i, 0);
			if(tid==id){
				ta.removeRow(i);
				break;
			}
		}
		ta.addRow(new String[]{null, null, null, null, null});
		
	}
	
	/*public void dynamicRefresh(Integer id){
		DefaultTableModel ta = (DefaultTableModel)table.getModel();
		String str = "";
		ZhUser user =new ZhUser();
		user.setuId(id);
		CommonResult<ZhUser>  com = zhUserService.selectStudent(user);
		user = com.getData();
		if(ov.getOvAudit() ==0){
			str="待审核";
		}else if(ov.getOvAudit() ==1){
			str="已通过";
		}else if(ov.getOvAudit() ==2){
			str="已驳回";
		}
		
		if(str.equals(comboBox_state.getSelectedItem())){
			if(kk ==null || (Integer)ta.getValueAt(kk, 0) ==ov.getOvId()){
				return;
			}
			ta.removeRow(table.getRowCount()-1);
			ta.insertRow(kk, new Object[]{
					ov.getOvId(),
					ov.getOvUname(),
					pum.selectPo(pum.selectUserId(ov.getOvUid()).getuPoid()),
					DateFormatUtil.getDateString(ov.getOvKtime(),"HH:mm")+"-"+DateFormatUtil.getDateString(ov.getOvJtime(),"HH:mm"),
					DateFormatUtil.getDateString(ov.getOvKtime(),"yyyy-MM-dd")
			});
		}else{
			for(int i=0;i<table.getRowCount();i++){
				Integer  tid = (Integer)ta.getValueAt(i, 0);
				if(tid !=null && tid==id){
					kk=i;
					ta.removeRow(i);
					break;
				}
			}
			ta.addRow(new String[]{null, null, null, null, null});
		}
			
	}*/
	
	
	
}
